--
-- Author: shunguo.chen
-- Date: 2019-07-01 20:36:56
--
-- AvatarShopModel
--

local AvatarShopInfo = import(".AvatarShopInfo")

local fileName = string.getModeName(...)
local ClassRef = class(fileName, app.mvc.ModelBase)

function ClassRef:ctor(data)
    self.super.ctor(self, data)
end

function ClassRef:init()
    self._shopInfos = nil
end

function ClassRef:clear()
    for i,shopInfo in ipairs(self._shopInfos) do
        shopInfo:clearNew()
    end
    UD.badge:setNum(Constant.FUNC_TYPE_AVATAR_SHOP, 0)
end

function ClassRef:getShopInfos()
    if not self._shopInfos then
        self._shopInfos = {}

        local class = UD:getClass()
        local conf = GD:getAvatarShopConf(class, 1)
        self._shopInfos[1] = AvatarShopInfo.new(conf)

        local conf = GD:getAvatarShopConf(class, 2)
        self._shopInfos[2] = AvatarShopInfo.new(conf)
    end
    return self._shopInfos
end

function ClassRef:checkRedPoint()
    local shopInfo = self._shopInfos[UD:getGender()]
    UD.badge:setNum(Constant.FUNC_TYPE_AVATAR_SHOP, shopInfo:isNew() and 1 or 0)
end

------------------------ receive ------------------------
-- 有新的商品解锁
function ClassRef:S2C_BarberUnSeeRedPointUpdate(msg)
    local shopInfos = self:getShopInfos()
    for i,shopInfo in ipairs(shopInfos) do
        shopInfo:unlockItem(msg)
    end
    
    self:checkRedPoint()

    app:sendMsg("AvatarShopController", "onUpdateShopItem")
end

------------------------ request ------------------------
-- 理发店商品激活状态
function ClassRef:C2S_BarberShopUnlock()
    -- print("C2S_BarberShopUnlock")
    app.session:request("pb_activity.C2S_BarberShopUnlock",{},function(msg)
        -- dump(msg, "C2S_BarberShopUnlock", 10)
        local shopInfos = self:getShopInfos()
        for i,shopInfo in ipairs(shopInfos) do
            shopInfo:initMsg(msg)
        end
    end)
end

-- 理发店商品激活状态
function ClassRef:C2S_HairCut(params)
    -- dump(params, "C2S_HairCut")
    app.session:request("pb_activity.C2S_HairCut",params,function(msg)
        -- dump(msg, "C2S_HairCut", 10)
        app:sendMsg("AvatarShopController", "onHairCut")
    end)
end

return ClassRef
